<?php
class Api_Lib_Rsp_Product {

    function __construct() {
        $this->_modelExample = FLEA::getSingleton('Model_Jichu_Product');
        $this->_modelKind = FLEA::getSingleton('Model_Jichu_ProKind');

        //根域名
        $_baseurl = detect_uri_base();
        $p = strrpos($_baseurl, '/');
        $this->baseurl = substr($_baseurl, 0,$p + 1);
    }

    /**
     * 获取产品分类列表
     * Time：2019/11/07 15:09:10
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function getKindList($params,& $service){
        $condition = array();
        if($params['kindName']!='') {
            $condition[] = array('kindName',"%{$params['kindName']}%",'like');
        }

        // $this->_modelExample->clearLinks();

        $rowset = $this->_modelKind->findAll($condition);
        //把需要的数据带出来
        $rows = array();
        foreach ($rowset as $key => & $v) {
            $v['extendAttr'] = $this->_modelKind->getFiledsKeyValue($v['id'] ,'all');
            $rows[] = array(
                'pkid'       => $v['id'],
                'kindName'   => $v['kindName'],
                'extendAttr' => $v['extendAttr'],
            );
        }

        return array('row'=>$rows);
    }

    /**
     * 获取产品分类列表
     * Time：2019/11/07 15:09:10
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function getProductList($params,& $service){
        $pageNum     = $params['pageNum'] ? $params['pageNum'] : 20;
        $currentPage = $params['page'] ? $params['page'] : 1;

        $condition = array();
        if($params['proCode']!='') {
            $condition[] = array('proCode',"%{$params['proCode']}%",'like');
        }
        if($params['proName']!='') {
            $condition[] = array('proName',"%{$params['proName']}%",'like');
        }
        if($params['key']!='') {
            // $condition[] = array('proName',"%{$params['key']}%",'like');
            $condition[] = " (proCode like '%{$params['key']}%' or proName like '%{$params['key']}%')";
        }
        if($params['kindId']!='') {
            $condition[] = array('kindId',$params['kindId'],'=');
        }
        $condition[] = array('isSample',1,'=');

        FLEA::loadClass('TMIS_Pager');
        $pager = & new TMIS_Pager($this->_modelExample,$condition,'id desc',$pageNum ,($currentPage - 1));
        $rowset = $pager->findAll();
        //把需要的数据带出来
        $rows = array();
        foreach ($rowset as $key => & $v) {
            $_kind = $this->_modelKind->find($v['kindId']);
            $v['Images'] = array();
            //获取图片地址
            if($v['pic']){
                $sql = "select path from jichu_image where id in ({$v['pic']})";
                $res = $this->_modelExample->findBySql($sql);
                foreach ($res as & $img) {
                    $v['Images'][] = $this->baseurl.$img['path'];
                }
            }

            //查找图片的url地址，使用带域名的地址
            $tmp = array(
                'pid'      => $v['id'],
                'proCode'  => $v['proCode'],
                'proName'  => $v['proName'],
                'memo'     => $v['memo'],
                'isSample' => $v['isSample'],
                'kindId'   => $v['kindId'],
                'kindName' => $_kind['kindName'],
                'pic'      => $v['pic'],
                'Images'   => $v['Images'],
                'others'   => array(),
            );

            //扩展字段
            $_index = 1;
            while ($_index < 21) {
                $tmp['filed'.$_index] = $v['filed'.$_index];
                $_index++ ;
            }
            $_tRow = array();
            $this->_modelExample->filedValue4($_tRow ,$tmp);
            foreach ($_tRow as $tk => & $desc) {
                $tmp['otherDesc'][] = $tk.':'.$desc;
                $tmp['others'][] = array('title'=>$tk  ,'value'=>$desc);
            }
            $tmp['othersRows'] = ceil(count($tmp['others'])/2);//用户计算显示其他信息的行数
            $tmp['otherDesc'] = join(' ',$tmp['otherDesc']);


            $rows[] = $tmp;
        }

        return array('row'=>$rows,'page'=>$currentPage,'total'=>$pager->totalCount,'pageCount'=>count($rows));
    }

    // 保存产品档案/更新产品档案
    function updateProduct($params,& $service){
        if(!$params['proName'] || !$params['proCode']){
            $service->send_user_error('proCode,proName required');
        }
        //检查是否存在
        $product = $this->_modelExample->find(array('proCode'=>$params['proCode']));
        //如果本地没有找到，则判断是否重复
        // if(!$product['id']) {
        //     $condition = array();
        //     $condition[] = array('proCode',$params['proCode'],'=');
        //     $count = $this->_modelExample->findCount($condition);

        //     if($count > 0) {
        //         $service->send_user_error('proCode repeat');
        //     }
        // } else {
        //     //修改时判断是否重复
        //     $condition = array();
        //     $condition[] = array('proCode',$params['proCode'],'=');
        //     $condition[] = array('id',$product['id'],'<>');
        //     $count = $this->_modelExample->findCount($condition);

        //     if($count > 0) {
        //         $service->send_user_error('proCode repeat');
        //     }
        // }

        //对图片数据进行处理下，屏蔽掉找不到的图片
        $params['pic'] = explode(',', $params['pic']);
        $imgArr = array();
        foreach ($params['pic'] as & $img) {
            $_imgId = intval($img);
            if($_imgId > 0){
                $sql = "select count(*) as cnt from jichu_image where id = '{$_imgId}'";
                $tmp = $this->_modelExample->findBySql($sql);
                if($tmp[0]['cnt'] > 0){
                    $imgArr[] = $_imgId;
                }
            }
        }
        $params['pic'] = join(',' ,$imgArr);

        //准备需要插入数据的数组
        $_data = array(
            'id'            => $product['id']+0,
            'proCode'       => $params['proCode'],
            'proName'       => $params['proName'],
            'kindId'        => intval($params['kindId']),
            'filed1'        => strval($params['filed1']),
            'filed2'        => strval($params['filed2']),
            'filed3'        => strval($params['filed3']),
            'filed4'        => strval($params['filed4']),
            'filed5'        => strval($params['filed5']),
            'filed6'        => strval($params['filed6']),
            'filed7'        => strval($params['filed7']),
            'filed8'        => strval($params['filed8']),
            'filed9'        => strval($params['filed9']),
            'filed10'       => strval($params['filed10']),
            'filed11'       => strval($params['filed11']),
            'filed12'       => strval($params['filed12']),
            'filed13'       => strval($params['filed13']),
            'filed14'       => strval($params['filed14']),
            'filed15'       => strval($params['filed15']),
            'filed16'       => strval($params['filed16']),
            'filed17'       => strval($params['filed17']),
            'filed18'       => strval($params['filed18']),
            'filed19'       => strval($params['filed19']),
            'filed20'       => strval($params['filed20']),
            'pic'           => $params['pic'],
            'memo'          => strval($params['memo']),
            'isSample'      => strval($params['isSample']) == '1' ? 1 : 0,
            'prefix'        => strval($params['proCode']),
            'materialPrice' => round($params['materialPrice'] , 6),
            'suffix'        => 0,
        );
        //数据保存
        $result = $this->_modelExample->save($_data);

        $product['id'] = $product['id'] ? $product['id'] : $result;

        return array('success'=>$result ? true :false ,'msg'=>'完成','data'=>array('id'=>$product['id']),'row'=>$_data);
    }

    //批量更新
    function updateProductLot($params,& $service){
        if(!$params['list']){
            $service->send_user_error('product list required');
        }

        $error = array();
        $success = array();
        foreach ($params['list'] as $key => & $v) {
            if(!$v['proName'] || !$v['proName']){
                $error[] = $key;
                continue;
            }

            //同步
            $res = $this->updateProduct($v ,$service);
            if($res['success']){
                $success[] = $key;
            }else{
                $error[] = $key;
            }
        }

        //返回数据的key键值数组
        return array('result'=>array('success'=>$success,'error'=>$error));
    }
}
?>